<?php
/**
 * Created by JetBrains PhpStorm.
 * User: MeRo
 * Date: 14.09.13
 * Time: 18:24
 * To change this template use File | Settings | File Templates.
 */

require_once 'templates/train/lib.php';
class start extends renderElement
{
    protected $menu = array(
        array('link' => 'new_surface',   'label' => 'Neue Landschaft',  'color' => 'random'),
        array('link' => 'load_surface',  'label' => 'Landschaft laden', 'color' => 'random'),
        array('link' => 'template',       'label' => 'Gleis-Vorlagen',   'color' => 'random'),
        array('link' => 'info',           'label' => 'Info',             'color' => 'random'),
        array('link' => 'exit',           'label' => 'Beenden',          'color' => 'random'),
    );

    function __construct() {
        $this
            ->createGrid()
            ->_render()
        ;
    }

    protected function createGrid() {
        $this->_addElement('<div class="gridly">');

        $menu       = $this->menu;
        $first      = true;
        $tile       = 0;
        $tooMuch    = 0;
        $colorsDone = array();

        while (count($menu) > 0) {
            $tile++;
            $type = (int) round(rand(0, 4));
            if ($type !== 1) {
                $tooMuch++;
            }

            if ($first === false && ($type !== 1 && $tooMuch < 4 || $tooMuch == 0)) {

                $this->_addElement('<div class="brick empty move"></div>');

            } else {
                $tooMuch    = 0;
                $pos        = (int) round(rand(0, count($menu) - 1));
                $pos        = 0;
                $color      = '';

                if (isset($menu[$pos]['color']) === true && $menu[$pos]['color'] == 'random') {
                    $color = getRandomColor(true, $colorsDone);
                    $colorsDone[] = $color;
                } elseif (isset($menu[$pos]['color']) === true) {
                    $color = $menu[$pos]['color'];
                }

                $this->_addElement(
                    '<div class="brick pointer" style="background-color: ' . $color . ';"><a href="' .
                        $menu[$pos]['link'] . '"><br /><br /><br />' . $menu[$pos]['label'] . '</a>
                        <div class="loadContainer"></div></div>'
                );
                unset($menu[$pos]);
                $menu = array_merge($menu);
                $first = false;
            }
        }

        $tileRemain = (int) 7 - ($tile % 7);
        $tileRemain = $tileRemain == 7 ? 0 : $tileRemain;
        for ($tileRemain; $tileRemain > 0; $tileRemain--) {
            $this->_addElement('<div class="brick empty"></div>');
        }

        $this->_addElement('</div>');

        return $this;
    }
}

new start;
